//
//  SampleViewController.m
//  DesighBook
//
//  Created by 梅明 on 15/9/14.
//  Copyright (c) 2015年 梅明. All rights reserved.
//

#import "SampleViewController.h"
#import "HttpRequestManager.h"
#import "DesighModel.h"
#import "UIImageView+AFNetworking.h"
#import "FirstTableViewCell.h"
#import "SecondTableViewCell.h"
#import "HeadView.h"
#import "MainViewController.h"
#import "SecondScrollViewController.h"
#import "NetInterface.h"
#import "MaintableDetailController.h"

#define viewwidth self.view.frame.size.width

@interface SampleViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
   CGFloat _oldoffset;
    BOOL _isHidden;
    CGFloat _naviheight;
}
/// 导航
@property (weak, nonatomic) IBOutlet UIView *Navigationview;

@property (weak,nonatomic) MainViewController *mainvc;
/// tableView
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic,strong) NSMutableArray *firstDataArray;
@property (nonatomic,strong) NSMutableArray *secondDataArray;
@property (nonatomic,strong) NSMutableArray *thirdDataArray;
@property (nonatomic,assign) CGFloat height1;
@property (retain, nonatomic)UIPanGestureRecognizer *panGesture;

@property (nonatomic, assign) CGFloat contentoffsetY;


@end

@implementation SampleViewController
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _firstDataArray = [[NSMutableArray alloc]init];
        _secondDataArray = [[NSMutableArray alloc]init];
        _thirdDataArray = [[NSMutableArray alloc]init];
        _oldoffset = 0;
        
    }
    return self;
}

-(NSMutableArray *)firstDataArray
{
    if (self==nil) {
        _firstDataArray=[NSMutableArray array];
    }
    return _firstDataArray;
}
-(void)viewWillAppear:(BOOL)animated
{
//    [self.navigationController setNavigationBarHidden:YES];

}
- (void)viewDidLoad {
    [super viewDidLoad];
//    _Navigationview.hidden = YES;
//    _myTableView.contentOffset = CGPointMake(0, 64);
//    _myTableView.contentInset = UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
    _myTableView.contentInset = UIEdgeInsetsMake(44, 0,viewwidth/5/4*3, 0);

    [self getDataFromNet];

}



-(void)getDataFromNet
{
    [[HttpRequestManager shareInstnce]getMainInfoSuccess:^(id responseObjec) {
        
        NSDictionary *dic1 = responseObjec;
        NSArray *firstarr = dic1[@"banner"];
        NSArray *secondarr = dic1[@"cases"];
        NSArray *thirdarr = dic1[@"jiexi"];
        
        //第一个数据源
        for (NSDictionary *dic in firstarr) {
            DesighModel *model = [[DesighModel alloc]init];
            model.imgViewurl = dic[@"imgUrl"];
            NSNumber *num1 = dic[@"id"];
            model.numberid =num1.stringValue;
            [_firstDataArray addObject:model];
        }
        //创建bannar的scrollview

        
        
        //第二个数据源
        for (NSDictionary *dic in secondarr) {
            DesighModel *model = [[DesighModel alloc]init];
            model.imgViewurl = dic[@"imgUrl"];
            NSNumber *num1 = dic[@"id"];
            model.numberid =num1.stringValue;
            model.faceimgurl = dic[@"facePic"];
            [model.styleArray addObject:dic[@"areaName"]];
            [model.styleArray addObject:dic[@"spaceName"]];
            [model.styleArray addObject:dic[@"styleName"]];
            [model.styleArray addObject:[NSString stringWithFormat:@"%@图",dic[@"counts"]]];
            model.name = dic[@"name"];
            [_secondDataArray addObject:model];
        }
        
        //第三个数据源
        for (NSDictionary *dic in thirdarr) {
            DesighModel *model = [[DesighModel alloc]init];
            model.imgViewurl = dic[@"imgUrl"];
            NSNumber *num1 = dic[@"id"];
            model.numberid =num1.stringValue;
            model.imgtitle1 = dic[@"imgTitle1"];
            model.imgtitle2 = dic[@"imgTitle2"];
            
            [_thirdDataArray addObject:model];
        }
        
        
        [_myTableView reloadData];
        
    } AndFailure:^(NSError *error) {
        NSLog(@"error == %@",error);
        
    }];
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section ==0) {
        return 1;
    }
    else
    {
        return _secondDataArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        //主页bannar,第一个scrollview
        FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firstcell"];
        [cell createFirstScrollView:_firstDataArray];
        
        //第二个scrollview
        [cell createSecondScrollView:_thirdDataArray];
        [cell setBlock:^(int i) {
            DesighModel *model =  _thirdDataArray[i-100];
            [self performSegueWithIdentifier:@"detail1" sender:model.numberid];

        }];
    
        return cell;
    }
    
    else
    {
    SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondcell"];
        DesighModel *model = _secondDataArray[indexPath.row];
        
        //主图
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapclick:)];
        cell.imgView.tag = 100 + indexPath.row;
        [cell.imgView addGestureRecognizer:tap1];
        [cell.imgView setImageWithURL:[NSURL URLWithString:model.imgViewurl]];
        cell.imgView.userInteractionEnabled = YES;
        
        //头像
        cell.whiteView.layer.cornerRadius = 30;
        [cell.faceImgview setImageWithURL:[NSURL URLWithString:model.faceimgurl]];
        cell.faceImgview.layer.cornerRadius =27;
        cell.faceImgview.clipsToBounds = YES;
        cell.title.text = model.name;
        
        
        for (int i = 0; i<model.styleArray.count; i++) {
            if (i==0) {
                cell.title2.text = model.styleArray[i];
            }
            else{
            NSString *str = model.styleArray[i];
            if (str.length == 0) {
                continue;
            }
            cell.title2.text = [NSString stringWithFormat:@"%@ / %@",cell.title2.text,model.styleArray[i]];
            }
        }
        return cell;
    }
  
}
-(void)tapclick:(UITapGestureRecognizer *)tap
{
    [self performSegueWithIdentifier:@"detail2" sender:tap];
}

//返回行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {

    return self.view.frame.size.width*0.5+self.view.frame.size.width*9/32+10;

    }
    return self.view.frame.size.width*171/320+75;;
}

//段头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section ==1) {
        HeadView *head = [[NSBundle mainBundle]loadNibNamed:@"HeadView" owner:nil options:nil][0];
        head.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
        return head;
    }
    
    return nil;
    
}
//段头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section ==1) {
        return 40;
    }
    return 0.00000000000001;
  
}

//段尾
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section ==1) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
        [button setTitle:@"查看全部设计方案" forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor whiteColor]];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont fontWithName:nil size:12];
        [view addSubview:button];
        return view;
    }
    return nil;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    _contentoffsetY = scrollView.contentOffset.y;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
    MainViewController *controll = (MainViewController *)self.tabBarController;

    if (_contentoffsetY < scrollView.contentOffset.y && self.Navigationview.frame.origin.y>-64) {
        _naviheight = 0 - (scrollView.contentOffset.y-_contentoffsetY);
        self.Navigationview.frame = CGRectMake(0, _naviheight, self.view.bounds.size.width, 64);
        CGFloat tabheight = (self.view.frame.size.height)-(viewwidth/5/4*3) + (scrollView.contentOffset.y - _contentoffsetY);
        controll.customTabbar.frame = CGRectMake(0,tabheight, viewwidth, viewwidth/5/4*3);
        if (scrollView.contentOffset.y-_contentoffsetY>64) {
            [UIView animateWithDuration:0.1 animations:^{
                _Navigationview.frame = CGRectMake(0, -64, viewwidth, 64);
                controll.customTabbar.frame = CGRectMake(0, self.view.frame.size.height, viewwidth, viewwidth/5/4*3);

                
            }];
        }
    
    }else if(_contentoffsetY > scrollView.contentOffset.y && self.Navigationview.frame.origin.y<0){
        
        _naviheight = -64 + (_contentoffsetY -scrollView.contentOffset.y);
        self.Navigationview.frame = CGRectMake(0, _naviheight, self.view.bounds.size.width, 64);
        CGFloat tabheight = self.view.frame.size.height + (scrollView.contentOffset.y - _contentoffsetY);
        controll.customTabbar.frame = CGRectMake(0,tabheight, viewwidth, viewwidth/5/4*3);
        if (_contentoffsetY - scrollView.contentOffset.y > 64) {
            [UIView animateWithDuration:0.1 animations:^{
                _Navigationview.frame = CGRectMake(0, 0, viewwidth, 64);
                controll.customTabbar.frame = CGRectMake(0, self.view.frame.size.height-(viewwidth/5/4*3), viewwidth, viewwidth/5/4*3);
            }];
        }
       
    }

}



-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"detail1"]) {
        SecondScrollViewController *view = segue.destinationViewController;
        view.numberid = sender;
    }
    
    if ([segue.identifier isEqualToString:@"detail2"]) {
        MaintableDetailController *view = segue.destinationViewController;
    
        UITapGestureRecognizer *tap = sender;
        DesighModel *model = _secondDataArray[tap.view.tag-100];
        view.numberid = model.numberid;
    }
}



@end
